package com.xisoft.common.db;

import com.xisoft.common.constant.IPubCons;
import com.xisoft.common.pubEnv.PubEnvUtil;
import com.xisoft.common.pubEnv.StringUtil;

/**
 * *
 * Created with Description:
 *
 * @version 1.0 <br>
 * @packagename: com.xisoft.common.db<br>
 * @author: LiBencheng <br>
 * @computer: licheng <br>
 * @date: Created on 2022/8/31 14:13 <br>
 */
public class DbTypeConvert
{

    /************************************************************* <br>
     *
     *说明: 整理 Db 类型 2 Java 类型<br>
     *
     * @Description   <br>
     * @Date Created on 2022/8/31  10:44  <br>
     * @param  <br>
     * @return java.lang.String  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static String collateDbType(String type, String fieldLength, String isPk)
    {
        if (PubEnvUtil.equals(IPubCons.NO, isPk.toLowerCase())) { type = "DXZJ"; }
        fieldLength = (PubEnvUtil.isEmpty(fieldLength)
                || PubEnvUtil.equals("DXZJ", type)) ? "36" : fieldLength;
        String dbType = "";
        switch (type)
        {
            case "DXZJ":
                dbType = "CHAR(" + fieldLength + ") NOT NULL ";
                break;
            case "Guid":
                dbType = "NVARCHAR2(" + fieldLength + ") ";
                break;
            case "Text":
                fieldLength = "3000";
                dbType = "NVARCHAR2(" + fieldLength + ") ";
                break;
            case "String":
                dbType = "NVARCHAR2(" + fieldLength + ") default '~'";
                break;
            case "string":
                dbType = "NVARCHAR2(" + fieldLength + ") default '~'";
                if (PubEnvUtil.equals("1", fieldLength)
                        || PubEnvUtil.equals("38", fieldLength))
                { dbType = "CHAR(" + fieldLength + ")"; }
                break;
            case "CHAR":
                dbType = "NVARCHAR2(" + fieldLength + ") default '~'";
                if (PubEnvUtil.equals("1", fieldLength)
                        || PubEnvUtil.equals("19", fieldLength)
                        || PubEnvUtil.equals("36", fieldLength)
                        || PubEnvUtil.equals("38", fieldLength))
                { dbType = "CHAR(" + fieldLength + ")"; }
                break;
            case "Int":
                dbType = "NUMBER(" + fieldLength + ")";
                break;
            case "number":
                dbType = "NUMBER(" + fieldLength + ")";
                break;
            case "Double":
                dbType = "DECIMAL(" + fieldLength + ",8)";
                break;
            case "Float":
                dbType = "FLOAT(" + fieldLength + ")";
                break;
            case "Date": //2022-10-01
                dbType = "CHAR(10)";
                break;
            case "Time": //2022-10-01 00:01:05
                dbType = "CHAR(19)";
                break;
            default:
                throw new RuntimeException(type + "沒有对应的数据类型");
        }

        return PubEnvUtil.SPACE + dbType;
    }

    /************************************************************* <br>
     *
     *说明:  整理 Db 类型 2 Java 类型<br>
     *
     * @Description   <br>
     * @Date Created on 2022/9/5  10:15  <br>
     * @param dataType
     * @param datascale <br>
     * @return java.lang.String  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static String collateDbType2JType(String dataType, String datascale)
    {
        String attrType = "";
        if (PubEnvUtil.equals("VARCHAR", dataType)
                || PubEnvUtil.equals("VARCHAR2", dataType)
                || PubEnvUtil.equals("NVARCHAR", dataType)
                || PubEnvUtil.equals("NVARCHAR2", dataType)
                || PubEnvUtil.equals("CHAR", dataType)
                || PubEnvUtil.equals("CLOB", dataType)
                || PubEnvUtil.equals("NCLOB", dataType)) { attrType = "java.lang.String"; }
        else if (PubEnvUtil.equals("DECIMAL", dataType)
                || (PubEnvUtil.equals("NUMBER", dataType)
                && Integer.valueOf(StringUtil.valueOfZero(datascale)) > 0)) { attrType = "java.math.BigDecimal"; }
        else if (PubEnvUtil.equals("BLOB", dataType)) { attrType = "java.lang.Object"; }
        else if (PubEnvUtil.equals("INTEGER", dataType) || PubEnvUtil.equals("INT", dataType)
                || PubEnvUtil.equals("NUMBER", dataType)) { attrType = "java.lang.Integer"; }
        else if (PubEnvUtil.equals("LONG", dataType)) { attrType = "java.lang.Long"; }
        else if (PubEnvUtil.equals("BOOLEAN", dataType)) { attrType = "java.lang.Boolean"; }
        else if (PubEnvUtil.equals("DATE", dataType)) { attrType = "java.util.Date"; }
        else if (PubEnvUtil.equals("DATETIME", dataType)) { attrType = "java.util.DateTime"; }
        else { attrType = "java.lang.String"; }

        return attrType;
    }

    /************************************************************* <br>
     *
     *说明: 整理 Db 类型 2 Java 类型<br>
     *
     * @Description   <br>
     * @Date Created on 2022/9/5  10:33  <br>
     * @param type
     * @param fieldLength <br>
     * @return java.lang.String  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static String collateJType2DbType(String type, String fieldLength)
    {
        fieldLength = (PubEnvUtil.isEmpty(fieldLength)
                || PubEnvUtil.equals("DXZJ", type)) ? "36" : fieldLength;
        String dbType = "";
        switch (type)
        {
            case "DXZJ":
                dbType = "NVARCHAR2(" + fieldLength + ") NOT NULL ";
                break;
            case "Text":
                fieldLength = "3000";
                dbType = "NVARCHAR2(" + fieldLength + ") ";
                break;
            case "String":
                dbType = "NVARCHAR2(" + fieldLength + ") default '~'";
                break;
            case "Int":
                dbType = "NUMBER(" + fieldLength + ")";
                break;
            case "Double":
                dbType = "NUMBER(" + fieldLength + ",8)";
                break;
            case "Float":
                dbType = "FLOAT(" + fieldLength + ")";
                break;
            case "Date": //2022-10-01
                dbType = "CHAR(10)";
                break;
            case "Time": //2022-10-01 00:01:05
                dbType = "CHAR(19)";
                break;
            default:
                throw new RuntimeException("沒有对应的数据类型");
        }

        return "";
    }

}
